package cn.dengta.webapp.user.entity;

import java.util.Date;
import java.util.Map;
import javax.annotation.Nullable;

import cn.dengta.common.model.SwitchSupport;
import cn.dengta.context.model.Language;
import cn.dengta.webapp.base.entity.BizEntity;
import cn.dengta.webapp.user.model.*;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;
import me.codeplayer.util.*;

/**
 * 合作链接表
 *
 * @author lgy
 * @since 2022-05-18
 */
@Setter
@Getter
@Accessors(chain = true)
public class Link extends BizEntity implements SwitchSupport {

	/** 点击次数 */
	private Integer hits;
	/** 语言 */
	private Language language;
	/** 开启时间 */
	private Date beginTime;
	/** 结束时间 */
	private Date endTime;
	/** 别名 */
	private String alias;
	/** 标签 */
	private String tag;
	/** 描述 */
	private String summary;
	/** logo */
	private String logo;
	/** 名称 */
	private String name;
	/** 排序 */
	private Integer sort;
	/** 类型 */
	private String type;
	/** 站点路径 */
	private String siteUrl;
	/** 关联类型 */
	protected String assocType;
	/** 关联id（多个ID以逗号隔开） */
	protected String assocIds;
	/** 添加人 */
	private Long addUserId;
	/** 性别 */
	private Gender gender;
	/** 客户端 */
	private String client;
	/** 规则JSON配置 */
	private String config;
	/** 分包 */
	private String wesKits;
	/** 已解析到的配置对象 */
	protected transient LinkConfig resolvedCfg;

	public Link init(Link input, Date now) {
		this.name = input.name;
		this.type = input.type;
		this.siteUrl = X.expectNotNull(input.siteUrl, "");
		this.language = X.expectNotNull(input.language, language);
		this.logo = StringUtil.toString(input.logo);
		this.summary = StringUtil.toString(input.summary);
		this.alias = X.expectNotNull(input.alias, alias, "");
		this.tag = X.expectNotNull(input.tag, tag, "");
		this.sort = X.expectNotNull(input.sort, sort, 0);
		this.hits = X.expectNotNull(input.hits, hits, 0);
		this.gender = input.gender;
		this.assocType = StringUtil.toString(input.assocType);
		this.assocIds = StringUtil.toString(input.assocIds);
		this.client = StringUtil.toString(input.client);
		this.addUserId = X.expectNotNull(addUserId, input.addUserId);
		this.beginTime = X.expectNotNull(input.beginTime, beginTime, now);
		this.endTime = input.endTime;
		this.wesKits = input.wesKits;
		this.config = StringUtil.toString(input.config);
		super.initTime(now);
		super.init(input.status, input.state);
		return this;
	}

	@Override
	public boolean isEnabled() {
		return this.status > 0;
	}

	public boolean isActivity(Long nowTime) {
		if (nowTime == null) {
			nowTime = System.currentTimeMillis();
		}
		return isActive(nowTime);
	}

	public String getStatusDesc(final long baseTime) {
		String desc = "已失效";
		if (!isEnabled()) {
			desc = "已失效"; // 已失效
		} else if (this.beginTime.getTime() > baseTime) {
			desc = "即将生效"; // 即将生效
		} else if (isActivity(baseTime)) {
			desc = "生效中"; // 使用中
		}
		return desc;
	}

	public static String getName_(String name) {
		return StringUtil.limitChars(name, 12);
	}

	public static String getSummary_(String summary) {
		return StringUtil.limitChars(summary, 12);
	}

	public LinkType linkTypeE() {
		return LinkType.of(type);
	}

	@Nullable
	public <T extends LinkConfig> T getResolvedCfg() {
		LinkConfig cfg = resolvedCfg;
		if (cfg == null) {
			resolvedCfg = cfg = linkTypeE().deserialize(config);
		}
		return X.castType(cfg);
	}

	public void updateCfg(LinkConfig taskRuleConfig) {
		this.config = linkTypeE().serialize(this.resolvedCfg = taskRuleConfig);
	}

	public <T extends LinkConfig> T updateResolvedCfg(String config) {
		this.resolvedCfg = linkTypeE().deserialize(this.config = config);
		return getResolvedCfg();
	}

	public static Map<String, Object> asEmbed(Link source) {
		return X.map(source, t -> CollectionUtil.ofHashMap("id", t.getId(), "name", t.getName()));
	}

	public interface LinkConfig extends java.io.Serializable {

		void validate();

		void init(LinkConfig config);

	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	public static final String HITS = "hits";
	public static final String BEGIN_TIME = "begin_time";
	public static final String END_TIME = "end_time";
	public static final String GENDER = "gender";
	public static final String CLIENT = "client";
	public static final String LANGUAGE = "language";
	public static final String ALIAS = "alias";
	public static final String TAG = "tag";
	public static final String SUMMARY = "summary";
	public static final String LOGO = "logo";
	public static final String NAME = "name";
	public static final String SORT = "sort";
	public static final String TYPE = "type";
	public static final String GLOBAL = "global";
	public static final String SITE_URL = "site_url";
	public static final String ADD_USER_ID = "add_user_id";
	public static final String WES_KITS = "wes_kits";

}
